% a example file to generate vtk file for pareview visualization
load ./D/715303298D_sos.mat
SaveVTK('sos_visual.vtk',sos, 0.1, [0 0 0], 'sound_speed');

function SaveVTK(filename, volume, voxel_size, org, array_name)
% Check file extension
if ~strcmp(filename(end-3:end), '.vtk') && ...
   ~strcmp(filename(end-3:end), '.VTK')
    error('Error: File extension is not ''.vtk'' or ''.VTK''.');
else
    [Nx, Ny, Nz] = size(volume);
    fid = fopen(filename, 'w');
    fprintf(fid, '# vtk DataFile Version 2.0\n');
    fprintf(fid, 'Results\n');
    fprintf(fid, 'ASCII\n');
    fprintf(fid, 'DATASET STRUCTURED_POINTS\n');
    fprintf(fid, 'DIMENSIONS %d %d %d\n', Nx, Ny, Nz);
    fprintf(fid, 'SPACING %f %f %f\n', voxel_size, voxel_size, voxel_size);
    fprintf(fid, 'ORIGIN %f %f %f\n', org(1), org(2), org(3));
    fprintf(fid, 'POINT_DATA %d\n', Nx*Ny*Nz);
    fprintf(fid, ['SCALARS ', array_name, ' float 1\n']);
    fprintf(fid, 'LOOKUP_TABLE default\n');
    fwrite(fid, num2str(volume(:)'));
    fclose(fid);
end